// timeStampTool.js
function getFirstAndLastMonthDay(year, month) {
	var firstdate = year + '-' + month + '-01';
	var day = new Date(year, month, 0);
	var lastdate = year + '-' + month + '-' + day.getDate();
	//获取当月最后一天日期
	//给文本控件赋值。同下
	return lastdate;
}

//获取当月的第一天
function getFirstMonthDay(year, month) {
	var firstdate = year + '-' + month + '-01';
	var day = new Date(year, month, 0);
	var lastdate = year + '-' + month + '-' + day.getDate();
	//获取当月最后一天日期
	//给文本控件赋值。同下
	return firstdate;
}

//型如2016-10-10转时间戳13
function timeStamp13WithString(timeString) {
	var tempStr = timeString.replace(/-/g, '/');
	console.log(tempStr);
	var newTime = new Date(tempStr);
	return newTime.getTime();
}

//型如2016-10-10转时间戳10
function timeStamp10WithString(timeString) {
	var tempStr = timeString.replace(/-/g, '/');
	var newTime = new Date(tempStr);
	return newTime.getTime() / 1000;
}

//获取当前月份最后一天0:00的时间戳
function getCurrentMonthLastDayTimeStamp() {
	var nowtime = new Date();
	var year = nowtime.getFullYear();
	var month = nowtime.getMonth();
	var timeString = getFirstAndLastMonthDay(year, month);
	return timeStamp13WithString(timeString);
}

//获取当前月份第一天0:00的时间戳
function getCurrentMonthFirstDayTimeStamp() {
	var nowtime = new Date();
	var year = nowtime.getFullYear();
	var month = nowtime.getMonth();
	var timeString = getFirstMonthDay(year, month);
	return timeStamp13WithString(timeString);
}

//获取当月最后一天X点的时间戳
function getCMLastDayTimeStamp(hour, minutes) {
	var timeStamp = getCurrentMonthLastDayTimeStamp();
	var tempTime = parseInt(timeStamp);
	tempTime += parseInt(hour) * 60 * 60 * 1000;
	tempTime += parseInt(minutes) * 60 * 1000;
	return tempTime;
}

//获取当天某点时间戳
function currentDayTimeWithHourMinutes(hour, minutes) {
	var signTimeStamp = currentDateTime() + hour * 60 * 60 * 1000 + minutes * 60 * 1000;
	return signTimeStamp;
}

//获取今天0:00时间戳
function currentDateTime() {
	var currentDate = new Date();
	// var currentDay = new Intl.DateTimeFormat('zh-cn').format(currentDate);
	var currentDay = currentDate.getFullYear() + '/' + (currentDate.getMonth() + 1) + '/' + currentDate.getDate();
	var currentDayTimeStamp = new Date(currentDay).getTime();
	return currentDayTimeStamp;
}

//获取当前周周X0:00的时间戳
function currentWeekTimeWithDay(weekDay) {
	var nowDateTime = currentDateTime();
	var currentDay = new Date(nowDateTime).getDay();
	if (weekDay >= currentDay) {
		var differenceDay = weekDay - currentDay;
		var differenceTime = differenceDay * 24 * 60 * 60 * 1000;
		return nowDateTime + differenceTime;
	} else {
		var differenceDay = currentDay - weekDay;
		var differenceTime = differenceDay * 24 * 60 * 60 * 1000;
		return nowDateTime - differenceTime;
	}
}

//获取当前周周X X点的时间戳
function currentWeekTimeWithDayHourMinutes(weekDay, hour, minutes) {
	return dateWithTimeHourMinutes(currentWeekTimeWithDay(weekDay), hour, minutes);
}

//获取某天某点时间戳
function dateWithTimeHourMinutes(timeStamp, hour, minutes) {
	return timeStamp + hour * 60 * 60 * 1000 + minutes * 60 * 1000;
}

//时间戳转时间
function timeWithStamp(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//获取当前时间
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	var timeHour = timeDate.getHours();
	if (timeHour < 10) {
		timeHour = '0' + timeHour;
	}
	var timeMinutes = timeDate.getMinutes();
	if (timeMinutes < 10) {
		timeMinutes = '0' + timeMinutes;
	}
	if (timeYear == currentYear) {//判断是否为同一年
		return timeMonth + '-' + timeDay + ' ' + timeHour + ':' + timeMinutes;
	} else {
		return timeYear + '-' + timeMonth + '-' + timeDay + ' ' + timeHour + ':' + timeMinutes;
	}
}

function timeWithStamp3(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//获取当前时间
	var currentDate = new Date();
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	var timeHour = timeDate.getHours();
	if (timeHour < 10) {
		timeHour = '0' + timeHour;
	}
	var timeMinutes = timeDate.getMinutes();
	if (timeMinutes < 10) {
		timeMinutes = '0' + timeMinutes;
	}
	return timeYear + '-' + timeMonth + '-' + timeDay + ' ' + timeHour + ':' + timeMinutes;
}

//时间戳转时间（转换成年月日）
function timeWithStamp2(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//获取当前时间
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	if (timeYear == currentYear) {//判断是否为同一年
		return timeMonth + '月' + timeDay + '日';
	} else {
		return timeYear + '年' + timeMonth + '月' + timeDay + '日';
	}
}
//时间戳转时间（转换成年月日）
function timeWithStamp6(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	return timeYear + '年' + timeMonth + '月' + timeDay + '日';
}
//时分
function timeWithStamp5(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//获取当前时间
	var currentDate = new Date();
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeHour = timeDate.getHours();
	if (timeHour < 10) {
		timeHour = '0' + timeHour;
	}
	var timeMinutes = timeDate.getMinutes();
	if (timeMinutes < 10) {
		timeMinutes = '0' + timeMinutes;
	}
	return timeHour + ':' + timeMinutes;
}

//今天、昨天、月日、年月日
function timeWithStamp4(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	//获取当前时间
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	//时间戳转换
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var todyStamp = currentDateTime();
	//获取今天的时间戳
	var tempTime = timeStampInt - todyStamp;
	var oneDayDuration = 24 * 60 * 60 * 1000;

	if (tempTime > 0 && tempTime < oneDayDuration) {//今天
		return '今天';
	} else if (tempTime < 0 && tempTime > (-oneDayDuration)) {//昨天
		return '昨天';
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	if (timeYear == currentYear) {//判断是否为同一年
		return timeMonth + '月' + timeDay + '日';
	} else {
		return timeYear + '年' + timeMonth + '月' + timeDay + '日';
	}
}

function timeStringWithTimeStamp(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	return timeYear + '-' + timeMonth + '-' + timeDay;
}

//返回以点分隔的时间
function timePointStringWithTimeStamp(timeStamp) {
	if (timeStamp == '' || timeStamp == null) {
		return '';
	}
	var timeStampInt = parseInt(timeStamp.toString());
	if (timeStamp.toString().length == 10) {
		timeStampInt = timeStampInt * 1000;
	}
	var timeDate = new Date(timeStampInt);
	var timeYear = timeDate.getFullYear();
	var timeMonth = timeDate.getMonth() + 1;
	if (timeMonth < 10) {
		timeMonth = '0' + timeMonth;
	}
	var timeDay = timeDate.getDate();
	if (timeDay < 10) {
		timeDay = '0' + timeDay;
	}
	return timeYear + '.' + timeMonth + '.' + timeDay;
}

function timeStamp10(date) {
	return timeStamp13(date) / 1000;
}

function timeStamp13(date) {
	return date.getTime();
}

//获取当前时间戳
function getNowDate() {
	return (new Date()).valueOf();
}

//获取当前的时间

function getNowDateYue() {
	var riqi = new Date();
	var year = riqi.getFullYear();
	//获取当前年
	var month = riqi.getMonth() + 1;
	//获取当前月
	var date = riqi.getDate();
	//获取当前日
	if (month < 10) {
		month = "0" + month;
	}
	if (date < 10) {
		date = "0" + date;
	}
	return year + month + date;
}
//时间字符串转时间戳
function timeStampWithTimeString(timeString){
	var date = new Date(timeString);
    var time = Date.parse(date)/1000;
    return time.toString();
}


  function diaplayTime(timeStamp) {
            	//时间戳转换
			var str = parseInt(timeStamp.toString());
			if (str.toString().length == 10) {
				str = str * 1000;
			}
            //将字符串转换成时间格式
            var timePublish = new Date(str);
            var timeNow = new Date();
            var minute = 1000 * 60;
            var hour = minute * 60;
            var day = hour * 24;
            var month = day * 30;
            var diffValue = timeNow - timePublish;
            var diffMonth = diffValue / month;
            var diffWeek = diffValue / (7 * day);
            var diffDay = diffValue / day;
            var diffHour = diffValue / hour;
            var diffMinute = diffValue / minute;

            if (diffValue < 0) {
                alert("错误时间");
            }
            else if (diffMonth > 3) {
                result = timePublish.getFullYear()+"-";
                result += timePublish.getMonth() + "-";
                result += timePublish.getDate();
                alert(result);
            }
            else if (diffMonth > 1) {
                result = parseInt(diffMonth) + "月前";
            }
            else if (diffWeek > 1) {
                result = parseInt(diffWeek) + "周前";
            }
            else if (diffDay > 1) {
                result = parseInt(diffDay) + "天前";
            }
            else if (diffHour > 1) {
                result = parseInt(diffHour) + "小时前";
            }
            else if (diffMinute > 1) {
                result = parseInt(diffMinute) + "分钟前";
            }
            else {
                result = "刚刚发表";
            }
            return result;
        }